package com.wx.spider.pc.controller;

import com.wx.spider.common.enums.MqMsgBean;
import com.wx.spider.common.model.CommonResponse;
import com.wx.spider.pc.config.CloudConfig;
import com.wx.spider.pc.socket.WxbizWebSocket;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:feng
 * @create:2021-04-23 12:09
 */
@RestController
@RequestMapping("/base")
public class BaseController {

    @Autowired
    private CloudConfig cloudConfig;

    @Autowired
    public RocketMQTemplate rocketMQTemplate;

    @Autowired
    private WxbizWebSocket wxbizWebSocket;


    @GetMapping("/socket")
    public CommonResponse<String> SendSocket(){
        try {
            wxbizWebSocket.sendMessage("run");
            return CommonResponse.succeed(cloudConfig.getPort());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }

    @GetMapping("/startSocket")
    public CommonResponse<String> startSocket(){
        try {
            System.out.println("startSocket:" + cloudConfig.getPort());
            rocketMQTemplate.convertAndSend(MqMsgBean.PM_TO_JAVA_SOCKET.getTopic(),"start tasks!");
            return CommonResponse.succeed(cloudConfig.getPort());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }
}
